package com.gx.smart.construction.ui.house.paging

import android.text.TextUtils
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.construction.ui.house.model.RoomInfo
import com.gx.smart.lib.http.api.ConstructionAppRoomInfoProviderService
import com.gx.smart.lib.http.api.asynctask.CallBack
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppGetAllRoomsInfoResponse
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfo
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

/**
 * @author: xiaosy
 * 时间: 2022/2/25
 * @Description: java类作用描述
 * @Version: 1.0
 */
class RoomInfoPagingSource(val type: Int) : PagingSource<Int, RoomInfo>() {

    override fun getRefreshKey(state: PagingState<Int, RoomInfo>): Int? = 0

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RoomInfo> {
        val currentPage:Int = params.key ?: START_INDEX
        val roomInfos = loadRoomList(currentPage, type)
        val prevKey =  if (currentPage <= START_INDEX) null else currentPage - 1
        val nextKey = if(roomInfos.isEmpty()) null else currentPage + 1
       return LoadResult.Page(roomInfos, prevKey, nextKey)

    }


    private suspend fun loadRoomList(currentPage: Int, type: Int):List<RoomInfo> = suspendCoroutine{
        val roomInfoList: MutableList<RoomInfo> = mutableListOf()
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(15)
            .build()
        ConstructionAppRoomInfoProviderService.getInstance()
            .getAllRoomsInfo(
                comQuery,
                type,
                object : CallBack<ConstructionAppGetAllRoomsInfoResponse?>() {
                    override fun callBack(result: ConstructionAppGetAllRoomsInfoResponse?) {
                        if (result == null) {
                            ToastUtils.showLong("服务器超时")
                            return
                        }
                        val comResp = result.comResp
                        if (comResp.code == 100) {
                            addConversionList(result.contentList, roomInfoList)
                            it.resume(roomInfoList)
                        } else {
                            val tip = comResp.tip
                            if (TextUtils.isEmpty(tip)) {
                                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                            } else {
                                ToastUtils.showLong(tip)
                            }
                        }
                    }
                })


    }

    private fun addConversionList(
        sourceList: List<ConstructionAppRoomInfo>?,
        targetList: MutableList<RoomInfo>
    ) {
        if (sourceList == null || sourceList.isEmpty()) {
            return
        }
        targetList.clear()
        for (item in sourceList) {
            targetList.add(
                RoomInfo(
                    item.id,
                    item.zoneName,
                    item.buildingName,
                    item.unitName,
                    item.number,
                    item.sn,
                )
            )
        }
    }
    companion object {
        private const val START_INDEX = 1
    }
}